package com.example.educate.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.educate.exception.EducateException;
import com.example.educate.model.vod.Teacher;
import com.example.educate.result.Result;
import com.example.educate.service.TeacherService;
import com.example.educate.vo.vod.TeacherQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author AT
 * @since 2022-09-16
 */
@Api(tags="讲师管理接口")
@RestController
@RequestMapping("/admin/vod/teacher")
//@CrossOrigin
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @GetMapping("findAll")
    @ApiOperation("查询所有讲师")
    public Result findAllTeacher(){
//        try{
//            int i=1/0;
//        } catch (Exception e){
//            throw new EducateException(201,"执行了AT设置处理异常");
//        }
        List<Teacher> list = teacherService.list();
        return Result.ok(list);
    }
    @DeleteMapping("remove/{id}")
    @ApiOperation("删除讲师")
    public Result removeTeacher(@ApiParam(name = "id",value = "ID") @PathVariable Long id){
        boolean isSuccess = teacherService.removeById(id);
        if(isSuccess){
            return Result.ok(null);
        }
        return Result.fail(null);
    }
    @PostMapping("findQueryPage/{current}/{limit}")
    @ApiOperation("条件查询")
    public Result findPage(@RequestBody(required = false) TeacherQueryVo teacherQueryVo, @PathVariable Long current, Long limit){
        //创建page对象
        Page<Teacher> page = new Page<>(current,limit);
        if (teacherQueryVo == null) {
            Page<Teacher> page1 = teacherService.page(page, null);
            return Result.ok(page1);
        }
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        String name = teacherQueryVo.getName();
        Integer level = teacherQueryVo.getLevel();
        String joinDateBegin = teacherQueryVo.getJoinDateBegin();
        String joinDateEnd = teacherQueryVo.getJoinDateEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(joinDateBegin)){
            wrapper.ge("join_date",joinDateBegin);
        }
        if(!StringUtils.isEmpty(joinDateEnd)){
            wrapper.le("join_date",joinDateEnd);
        }
        Page<Teacher> page1 = teacherService.page(page,wrapper);
        return Result.ok(page1);
    }
    //添加讲师
    @ApiOperation("添加讲师")
    @PostMapping("saveTeacher")
    public Result saveTeacher(@RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if(save){
            return Result.ok(null);
        }else{
            return Result.fail(null);
        }
    }
    //根据id查询讲师
    @ApiOperation("根据id查询讲师")
    @GetMapping("getTeacher/{id}")
    public Result getTeacher(@PathVariable Long id){
        Teacher user = teacherService.getById(id);
        return Result.ok(user);
    }
    //修改最终实现
    @ApiOperation("修改讲师")
    @PostMapping("updateTeacher")
    public Result updateTeacher(@RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if(b){
                return Result.ok(null);
            }else{
                return Result.ok(null);
        }
    }
    //批量删除讲师
    @ApiOperation("批量删除讲师")
    @DeleteMapping("deleteBatch")
    public Result removeBatch(@RequestBody List <Long> idList){
        boolean b = teacherService.removeByIds(idList);
        if(b){
            return Result.ok(null);
        }else{
            return Result.ok(null);
        }
    }
}

